package com.erlin.platform.service.impl;

import com.erlin.common.config.OssConfig;
import com.erlin.common.vo.FileUploadResultVo;
import com.erlin.platform.entity.ElFile;
import com.erlin.platform.mapper.ElFileMapper;
import com.erlin.platform.service.IElFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erlin.platform.service.bo.FileUploadBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author alin
 * @since 2021-05-17
 */
@Service
public class ElFileServiceImpl extends ServiceImpl<ElFileMapper, ElFile> implements IElFileService {
    @Autowired
    private OssConfig ossConfig;

    @Override
    public FileUploadResultVo insertFile(FileUploadBo fileUploadBo) {
        ElFile target = new ElFile();
        MultipartFile multipartFile = fileUploadBo.getMultipartFile();
        String filename = multipartFile.getOriginalFilename();
        target.setFileName(filename);
        assert filename != null;
        target.setFileClas(filename.substring(filename.lastIndexOf(".") + 1, filename.length()));
        target.setFileSize(multipartFile.getSize());
        target.setBucket(ossConfig.getBucket());
        target.setFolder(ossConfig.getFolder());
        target.setRelativePath(fileUploadBo.getRelativePath());
        var url = ossConfig.getDownloadUrl() + fileUploadBo.getRelativePath();
        target.setUrl(url);
        save(target);
        return new FileUploadResultVo().from(target);
    }
}
